package gen.desktop.components;

import gen.core.Generator;
import gen.core.exceptions.GenException;
import gen.core.models.ConnConfig;
import gen.core.utils.GenUtils;
import gen.desktop.Application;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

public class GenBySqlPanel extends JPanel implements TemplateToolBarResponser{
	private static final String CATAGORY = "GenBySql";
	private static final long serialVersionUID = 1L;
	private JTextArea textAreaTemplate;
	private JTextArea textAreaResult;
	private JSplitPane splitPaneMain;
	private JSplitPane splitPaneSub;
	private JButton btnGenButton;
	private JTextArea textAreaSql;

	/**
	 * Create the panel.
	 */
	public GenBySqlPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		add(toolBar, BorderLayout.NORTH);
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		toolBar.add(separator);
		
		btnGenButton = new JButton("生成");
		btnGenButton.setIcon(new ImageIcon(GenBySqlPanel.class.getResource("/gen/desktop/icons/size16x16/cog.png")));
		btnGenButton.setFont(new Font("宋体", Font.PLAIN, 12));
		toolBar.add(btnGenButton);
		
		splitPaneMain = new JSplitPane();
		splitPaneMain.setOrientation(JSplitPane.VERTICAL_SPLIT);
		add(splitPaneMain, BorderLayout.CENTER);
		
		splitPaneSub = new JSplitPane();
		splitPaneMain.setRightComponent(splitPaneSub);
		
		JPanel panel_1 = new JPanel();
		splitPaneSub.setLeftComponent(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		TemplateToolBar toolBar_1 = new TemplateToolBar(this, CATAGORY);
		toolBar_1.setRollover(false);
		toolBar_1.setFloatable(false);
		panel_1.add(toolBar_1, BorderLayout.NORTH);
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane);
		
		textAreaTemplate = new JTextArea();
		scrollPane.setViewportView(textAreaTemplate);
		
		JPanel panel_2 = new JPanel();
		splitPaneSub.setRightComponent(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_2.add(scrollPane_1, BorderLayout.CENTER);
		
		textAreaResult = new JTextArea();
		scrollPane_1.setViewportView(textAreaResult);
		
		JPanel panel = new JPanel();
		splitPaneMain.setLeftComponent(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane_2 = new JScrollPane();
		panel.add(scrollPane_2, BorderLayout.CENTER);
		
		textAreaSql = new JTextArea();
		scrollPane_2.setViewportView(textAreaSql);

	}
	
	public void init(){
		centerDivider();

		// 相应生成按钮，生成代码
		btnGenButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sql = GenBySqlPanel.this.textAreaSql.getText();
				String templateString = GenBySqlPanel.this.textAreaTemplate.getText();

				Generator aGenerator = new Generator();
				Connection conn = null;
				try {
					ConnConfig aConnConfig = Application.getMainFrame().getMainMenuBar().getCurConnConfig();
					if(aConnConfig==null){
						throw new GenException(null, "当前连接未设置！");
					}
					
					conn = GenUtils.createConnection(aConnConfig.toProperties());
					aGenerator.init();
					String result = aGenerator.genBySql(conn, sql, templateString);
					GenBySqlPanel.this.textAreaResult.setText(result);
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage(), "代码生成失败！", JOptionPane.ERROR_MESSAGE);
					return;
				}finally{
					GenUtils.closeConnection(conn);
				}
			}
		});
	}

	@Override
	public void setTemplateText(String templateStr) {
		textAreaTemplate.setText(templateStr);
	}

	@Override
	public void setResultText(String resultStr) {
		textAreaResult.setText(resultStr);
	}

	public JTextArea getTextAreaTemplate() {
		return textAreaTemplate;
	}
	public JTextArea getTextAreaResult() {
		return textAreaResult;
	}
	
	public void centerDivider() {
		splitPaneMain.setDividerLocation(0.25);
		splitPaneSub.setDividerLocation(0.5);
	}
	
	public JSplitPane getSplitPaneMain() {
		return splitPaneMain;
	}
	public JSplitPane getSplitPaneSub() {
		return splitPaneSub;
	}

	@Override
	public String getTemplateText() {
		return this.textAreaTemplate.getText();
	}

	@Override
	public String getResultText() {
		return this.textAreaResult.getText();
	}
	public JButton getBtnGenButton() {
		return btnGenButton;
	}

	@Override
	public void setSqlText(String sqlStr) {
		this.textAreaSql.setText(sqlStr);
	}

	@Override
	public String getSqlText() {
		return this.textAreaSql.getText();
	}
}
